/*
 * @Author:Iyoole
 * @Time  :2017-11-20
 * @Description:分页
 * @git:
 * */

;!function(e){
    "use strict";
    var i,o={
        m:5,
        l:10
    };
    var Page=function(elem){
        this.elem=elem;
    };
    Page.pt=Page.prototype;
    Page.pt.render=function(a,c,f){
        var t=this,m=t.elem.attr('page-max'),l=t.elem.attr('page-list');
        m=m!=""&&m!=null&&m>0?parseInt(m):o.m;
        l=l!=""&&l!=null&&l>0?parseInt(l):o.l;
        a=(typeof a==='number')?a>0?a:0:0;
        c=(typeof c==='number')?c>=0?c:1:1;
        t.config={a:a,c:c,m:m,l:l};
        t.view();
        t.fun(f);
    };
    Page.pt.view=function(){
        var t=this,tc=t.config,n=tc.a%tc.l>0?(Math.floor(tc.a/tc.l) +1):tc.a/tc.l;
        var pre=tc.c - 1,next=tc.c + 1;
        var s='<li class="'+(pre<=0?"pageDisabled":"pageItem")+'" page-data="1" page-rel="firstpage">首页</li>';
        s+='<li class="'+(pre<=0?"pageDisabled":"pageItem")+'" page-data="'+pre+'" page-rel="prepage">&lt;</li>';
        var ms=Math.floor(tc.m/2),mn=1;
        if(tc.c-ms>0&&tc.c+ms<=n){
            mn=tc.c-ms
        }else if(tc.c-ms>0&&tc.c+ms>n){
            mn=n-tc.m+1;
            mn=mn<=0?1:mn;
        }
        var sn=n<tc.m?n:tc.m;
        for(var x=0;x<sn;x++){
            var nn=mn++;
            s+='<li class="'+(nn==tc.c?"pageItemActive":"pageItem")+'" page-data="'+nn+'" page-rel="itempage">'+nn+'</li>'
        }
        s+='<li class="'+(next>n?"pageDisabled":"pageItem")+'" page-data="'+next+'" page-rel="nextpage">&gt;</li>';
        s+='<li class="'+(next>n?"pageDisabled":"pageItem")+'" page-data="'+n+'" page-rel="lastpage">尾页</li>';
        t.elem.html('<ul class="JsPage">'+s+'</ul>')
    };
    Page.pt.fun=function(f){
        var t=this;
        i(t.elem).on('click','.pageItem',function(){
            var ck=i(this).attr('page-data');
            if(typeof f==='function'){
                f(ck);
            }
            t.config['c']=parseInt(ck);
            t.view();
        });
    };
    var JsPage=function(elem){
        elem=!!elem?i(elem):i('body');
        return new Page(elem)
    };
    JsPage.v='1.0.1';
    if(window.fui && fui.define ){
        fui.define('jquery', function(exports){
            e.JsPage=JsPage;
            i=fui.$;
            exports('JsPage', JsPage);
        });
    }else{//如果直接引用的话
        e.JsPage=JsPage;
        i=e.jQuery;
    }
}(window);


/*
* 说明
* JsPage('元素').render(总条数,当前选中页码,回调方法)
*
* <div class="page" page-max="m" page-list="n"></div>
* 最大显示数为m 每页有n条  默认m为5,n为10
* */
/*
* 更新
* 1.0.2
* 修复非除个数时引起的多一页问题(添加取整)
* */









